<?php
/**
 * 素材管理
 * 
 * @version $Id$
 */

class Module_material
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}

	
	function image() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		
		$smarty = loadSmarty();
		
		$perpage = 15;
		$offset = ($page - 1) * $perpage;
		
		$Db = loadLib('Db');
		$list = $Db->getAll("SELECT * FROM `material` ORDER BY `id` DESC LIMIT {$offset} , {$perpage}");
		
		foreach($list as $k => $row) {
			$list[$k]['size'] = sprintf('%.1f' , $row['size'] / 1024);
		}
		
		// 计算总数
		$count = $Db->getOne("SELECT COUNT(*) AS `count` FROM `material`");
		$image_count = $count['count'];
		
		$smarty->assign('list' , $list);
		$smarty->assign('image_count' , $image_count);
		$smarty->assign('perpage' , $perpage);
		
		return $smarty->fetch('material/image.tpl.php');
	}
	
	/**
	 * 上传图片
	 */
	function upimage() {
		$upimg_folder_prefix = isset($_POST['upimg_folder_prefix']) ? trim($_POST['upimg_folder_prefix']) : null;
		$upimg_subfolder = isset($_POST['upimg_subfolder']) ? trim($_POST['upimg_subfolder']) : '';
		$upimg_random_name = isset($_POST['upimg_random_name']) && !empty($_POST['upimg_random_name']) ? true : false;
		$upimg_width = isset($_POST['upimg_width']) ? intval($_POST['upimg_width']) : 0;
		$upimg_height = isset($_POST['upimg_height']) ? intval($_POST['upimg_height']) : 0;
		$overflow = false;
		
		$files = isset($_FILES['upimg']) ? $_FILES['upimg'] : null;
		
		if(empty($files) || empty($files['name'])) {
			return View::error('没有选择要上传的照片');
		}
		
		if(!empty($files['error'])) {
			return View::error('上传出错了，错误码：' . $files['error']);
		}
		
		if(empty($upimg_folder_prefix)) {
			return View::error('参数错误，没有选择目录');
		}
		
		// 拼目录
		$upimg_folder_prefix = trim($upimg_folder_prefix , '/ ');
		$upimg_subfolder = trim($upimg_subfolder , '/ ');
		$target_folder = '/' . $upimg_folder_prefix . '/' . (empty($upimg_subfolder) ? '' : $upimg_subfolder . '/');
		$target_full_folder = ROOT_PATH . '/web/image' . $target_folder;
		
		$name = $files['name'];
		if($upimg_random_name) {
			$img_type = strtolower(substr(strrchr($name , '.') , 1));
			$name = 'rand_' . rand(1000000 , 9999999) . '.' . $img_type;
		}
		
		$target_url = $target_full_folder . $name;
		
		// 判断图片是否已经存在
		if(!$overflow) {
			if(file_exists($target_url)) {
				return View::error('该文件已存在');
			}
		}
		
		// 生成
		loadLib('Thumb');
		$img_info = Thumb::genarate($files['tmp_name'] , $target_full_folder , $name , $upimg_width , $upimg_height);
		
		if(empty($img_info) || !file_exists($target_url)) {
			return View::error('上传失败');
		}
		
		$imageinfo = getimagesize($target_url);
		$width = $imageinfo[0];
		$height = $imageinfo[1];
		$size = filesize($target_url);
		
		$Db = loadLib('Db');
		$insert_array = array(
			'name' => $target_folder . $img_info['name'],
			'time' => time(),
			'uin' => $_SESSION['adminid'],
			'size' => $size,
			'width' => $width,
			'height' => $height,
		);
		$success = $Db->doInsert('material' , $insert_array);
		
		if(!success) {
			return View::error('写入数据库失败');
		}
		
		header('Location: ' . ADMINURL . '/?m=material&a=image');
	}


	function delete() {
		$image_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : null;

		if(empty($image_id)) {
			return Format::json_return(1 , '参数错误');
		}

		$Db = loadLib('Db');

		// 获取图片地址
		$image = $Db->getOne("SELECT * FROM `material` WHERE `id` = " . $image_id);
		if(empty($image)) {
			return Format::json_return(1 , '该数据在数据库中不存在');
		}

		$name = $image['name'];
		$target_image = ROOT_PATH . '/web/image' . $name;
		if(!file_exists($target_image)) {
			return Format::json_return(1 , '该图片不存在');
		}

		unlink($target_image);


		$Db->query("DELETE FROM `material` WHERE `id` = " . $image_id);

		return Format::json_return(0 , '删除成功');
	}
	/**
	 * 道具管理初始化
	 */
	function game_prop(){
		$game = isset($_GET['game']) ? trim($_GET['game']) : 'app_kingdoms_prop';
		$smarty = loadSmarty();
		loadLib('App_prop');
		/******************道具类型****************/
		$type = App_prop::getGameProp($game);
		$list = App_prop::getAllProp($game);
		$smarty->assign('list',$list);
		$smarty->assign('game',$game);
		$smarty->assign('type',$type);
		return $smarty->fetch('material/game_prop.tpl.php');
	}
	/**
	 * 道具添加初始化
	 */
	function prop(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$game = isset($_GET['game']) ? trim($_GET['game']) : '';
		
		$smarty = loadSmarty();
		
		if (!empty($id) && !empty($game)){
			loadLib('App_prop');
			$list = App_prop::getOneProp($game, $id);
			if (empty($list)){
				return View::error('该道具不存在!!!');
			}
			/******************道具类型****************/
			$type = App_prop::getGameProp($game);
			
			$smarty->assign('list',$list);
			$smarty->assign('type',$type);
		}
		//print_r($list);
		$smarty->assign('game',$game);
		return $smarty->fetch('material/prop.tpl.php');
	}
	/**
	 * 道具添加
	 */
	function addprop(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$game = isset($_POST['game']) ? trim($_POST['game']) : '';
		$prop = isset($_POST['prop']) ? trim($_POST['prop']) : '';
		$name = isset($_POST['name']) ? trim($_POST['name']) : '';
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		$num = isset($_POST['num']) ? intval($_POST['num']) : 0;
		$price = isset($_POST['price']) ? intval($_POST['price']) : null;
		
		if (empty($game)){
			return Format::json_return(1 , '没有选择 游戏名称');
		}
		if (empty($prop)){
			return Format::json_return(1 , '没有选择 道具类型');
		}
		if (empty($name)){
			return Format::json_return(1 , '没有填写 道具名称');
		}
		if (empty($price)){
			return Format::json_return(1 , '没有填写 道具价格');
		}
		if (empty($content)){
			return Format::json_return(1 , '没有填写 道具描述');
		}
		loadLib('App_prop');
		$uin=$_SESSION['adminid'];
		$list = App_prop::getOneProp($game, $id);
		
		//return Format::json_return(1 , $id);
		if (empty($list)){
			/*********************插入游戏道具操作**************************/
			App_prop::getAddProp($name, $price, $prop, $num, $content, $game , $uin);
			$prompt = '添加';
		}else{
			/*********************修改游戏道具操作**************************/
			App_prop::getAlterGameProp($list['id'], $name, $price, $prop, $num, $content, $game , $uin);
			$prompt = '修改';
		}
		return Format::json_return(0 , '道具('.$name.')'.$prompt.'成功!!!',$game);
		
	}
	/**
	 * 删除道具
	 */
	function porpdelete() {
		$prop_id = isset($_POST['prop_id']) ? intval($_POST['prop_id']) : null;
		$game = isset($_POST['game']) ? trim($_POST['game']) : '';
		//return Format::json_return(1 , $game);
		if(empty($prop_id) || empty($game)) {
			return Format::json_return(1 , '参数错误');
		}
		//return Format::json_return(1 , $game);
		$Db = loadLib('Db');
		$Db->query("DELETE FROM {$game} WHERE `id` = " . $prop_id);

		return Format::json_return(0 , '删除成功');
	}
}

# end of script
